﻿using Admin.NET.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MOKA.TPM.Application.Enum;
using MOKA.TPM.Application.Service;
using MOKA.TPM.Application.Service.Dto;
using MOKA.TPM.Entity;
using MOKA.TPM.Service.Dto;
using MOKA.TPM.Utility.Const;
using Newtonsoft.Json.Linq;
using SqlSugar;

namespace MOKA.TPM.Service;

/// <summary>
/// TPM业务逻辑控制器
/// </summary>
[ApiDescriptionSettings(TPMConst.GroupName, Order = 305)]
[NonUnify]
public class TPMBaseService : IDynamicApiController, ITransient
{
    protected UserManager _userManager;
    protected ISqlSugarClient _db;
    protected Service_SysDictDetail _dicService;
    protected IHttpContextAccessor _accessor;//获取请求上下文_userRepository

    public TPMBaseService(UserManager userManager, Service_SysDictDetail dicService, IHttpContextAccessor accessor, ISqlSugarClient db)
    {
        _userManager = userManager;
        _accessor = accessor;
        _dicService = dicService;
        _db = db;
        //_db =  _db.AsTenant().GetConnectionScope(TPMConst.TPMConfigId);
    }

    /// <summary>
    /// 根据登录用户获取车间列表
    /// </summary>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "GetWorkshopData"), HttpPost]
    [AllowAnonymous]
    public async Task<CommonResult> GetWorkshopByUser(TPMPara obj)
    {
        CommonResult result = new CommonResult();
        try
        {
            string UserName = obj.UserName.ToString();

            var Workshops = await _dicService.GetDetailListByDictCode("Workshop", TPMDeviceGroupEnum.MROGroup);
            if (Workshops != null && Workshops.Count > 0)
            {
                if (UserName != "admin")
                    result.Data = Workshops.FindAll(p => (p.ItemName3 ?? "").ToLower().Contains(UserName.ToLower())
                                                 );//车间字典，有权限控制
                else
                    result.Data = Workshops;
                result.Result = true;
            }
        }
        catch (Exception ex)
        {
            result.Result = false;
            result.Error = ex.Message.ToString();
        }
        return result;
    }

    /// <summary>
    /// 根据车间，获取仓位列表
    /// </summary>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "GetLocationData"), HttpPost]
    [AllowAnonymous]
    public async Task<CommonResult> GetLocationByWorkshop(TPMPara obj)
    {
        CommonResult result = new CommonResult();
        if (obj.Workshop == null)
        {
            result.Result = false;
            result.Error = "请先选择车间";
            return result;
        }
        try
        {
            List<SysDictDetailOutput> dtLocation = await _dicService.GetDetailListByItemParent("Location", obj.Workshop.Value, TPMDeviceGroupEnum.MROGroup);

            if (dtLocation != null && dtLocation.Count > 0)
            {
                result.Data = dtLocation;
                result.Result = true;
            }
        }
        catch (Exception ex)
        {
            result.Result = false;
            result.Error = ex.Message.ToString();
        }
        return result;
    }

    /// <summary>
    /// 根据车间，获取产线列表
    /// </summary>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "GetFrom_LineData"), HttpPost]
    [AllowAnonymous]
    public async Task<CommonResult> GetLineByWorkshop(TPMPara obj)
    {
        CommonResult result = new CommonResult();
        if (obj.Workshop == null)
        {
            result.Result = false;
            result.Error = "请先选择车间";
            return result;
        }
        try
        {
            List<SysDictDetailOutput> dtLine = await _dicService.GetDetailListByItemParent("Line", obj.Workshop.Value, TPMDeviceGroupEnum.MROGroup);

            //foreach (var line in dtLine)
            //{
            //    if (line.ItemValue.Contains("*"))
            //    {
            //        dtLine = dal.GetLines();
            //        dtLine.RemoveAll(p => p.ItemValue.Contains("*"));
            //        break;
            //    }
            //}
            if (dtLine != null && dtLine.Count > 0)
            {
                result.Data = dtLine;
                result.Result = true;
            }
        }
        catch (Exception ex)
        {
            result.Result = false;
            result.Error = ex.Message.ToString();
        }
        return result;
    }

    /// <summary>
    /// 根据车间，获取产线列表
    /// </summary>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "GetTo_LineData"), HttpPost]
    [AllowAnonymous]
    public async Task<CommonResult> GetTo_LineData(TPMPara obj)
    {
        return await GetLineByWorkshop(obj);
    }

    /// <summary>
    /// 根据类别名获取字典数据
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "GetDictDatByCatCode"), HttpPost]
    [AllowAnonymous]
    public async Task<CommonResult> GetDictDatByCatCode(TPMPara obj)
    {
        CommonResult result = new CommonResult();

        try
        {
            string CatCodes = obj.CatCodes.ToString(); //"TPMCategoryId,Position"

            var dt = await _dicService.GetDetailListByDictCodes(CatCodes, TPMDeviceGroupEnum.MROGroup);

            if (dt != null && dt.Count > 0)
            {
                result.Data = dt;
                result.Result = true;
            }
        }
        catch (Exception ex)
        {
            result.Data = null;
            result.Result = false;
            result.Error = ex.Message.ToString();
        }
        return result;
    }

    public class TPMPara
    {
        public Guid? Workshop { get; set; }

        public string? UserName { get; set; }
        public string? CatCodes { get; set; }
    }
}